package com.tinem.platform.web.auth.page;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.tinem.platform.module.pojo.enums.rbac.GenderEnum;
import com.tinem.platform.module.pojo.enums.rbac.LoginType;
import com.tinem.platform.module.starter.jpa.entity.oauth.Oauth2WechatOpenUserInfo;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.core.user.OAuth2User;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * @author fengzhihao
 * 登录的用户信息
 */
@Data
public class LoginUserInfo implements OAuth2User, Serializable,UserDetails {
    private String userId;
    private String username;
    private String name;
    private String nickName;
    private String headImage;
    private GenderEnum sex;
    private String account;
    private String countryMobileCode;
    private String mobile;
    private String email;
    @JSONField(serialize = false)
    private Date expiredDate;
    @JSONField(serialize = false)
    private Date lockedDate;
    @JSONField(serialize = false)
    private boolean enabled;
    @JSONField(serialize = false)
    private String password;
    @JSONField(serialize = false)
    private Integer passwordErrorNumber;
    @JSONField(serialize = false)
    private Date passwordErrorTime;
    @JSONField(serialize = false)
    private Date passwordExpired;

    /**
     * 用户权限
     */
    Collection<GrantedAuthority> authorities;
    /**
     * 登录方式
     */
    LoginType loginType;
    /**
     * 单点登录用户信息
     */
    @JSONField(serialize = false)
    OAuth2User oAuth2User;

    @JSONField(serialize = false)
    Oauth2WechatOpenUserInfo oauth2WechatOpenUserInfo;

    @Override
    public String getName(){
        return StrUtil.isNotBlank(this.name)?this.name:this.getNickName();
    }

    /**
     * 返回授予用户的权限。
     *
     * @return a {@code Collection} of {@link GrantedAuthority}(s)
     */
    @Override
    @JSONField(serialize = false)
    public Collection<? extends GrantedAuthority> getAuthorities(){
        return authorities;
    }
    /**
     * 指示用户的帐户是否已过期。
     * @return
     */
    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonExpired() {
        return this.getExpiredDate().after(new Date());
    }

    /**
     * 指示用户是锁定还是解锁。
     * @return
     */
    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonLocked() {
        return new Date().after(this.getLockedDate());
    }

    /**
     *指示用户的凭据（密码）是否已过期。
     * @return
     */
    @Override
    @JSONField(serialize = false)
    public boolean isCredentialsNonExpired() {
        return this.getPasswordExpired().after(new Date());
    }

    /**
     * Returns the attributes about the user.
     *
     * @return a {@code Map} of attributes about the user
     */
    @Override
    @JSONField(serialize = false)
    public Map<String, Object> getAttributes(){
        if(oAuth2User == null){
            return null;
        }
        return this.oAuth2User.getAttributes();
    }

}
